<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'

const animationDuration = 1000

export default {
  props: {
    className: {
      type: String,
      default: 'chart',
    },
    width: {
      type: String,
      default: '100%',
    },
    height: {
      type: String,
      default: '450px',
    },
    chartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        if (val) {
          this.setOptions(val)
        }
      },
    },
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'wonderland')
      this.setOptions(this.chartData)
    },
    setOptions(chartData) {
      if (!this.chart) {
        return
      }
      let option = {}
      if (!chartData || Object.keys(chartData).length === 0 || chartData.empty === true) {
        option = {
          color: ['#68cee0', 'rgba(104, 206, 224, 0.8)', 'rgba(104, 206, 224, 0.6)', 'rgba(104, 206, 224, 0.4)', 'rgba(104, 206, 224, 0.2)', 'rgba(255, 255, 255, 0.2)', 'rgba(255, 255, 255, 0.4)', 'rgba(255, 255, 255, 0.6)'],
          xAxis: {
            data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            boundaryGap: false,
            axisTick: {
              show: false,
            },
            axisLabel: {
              color: '#ffffff',
            },
            axisLine: {
              lineStyle: {
                color: '#ffffff',
              },
            },
          },
          grid: {
            left: 10,
            right: 10,
            bottom: 20,
            top: 30,
            containLabel: true,
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross',
            },
            padding: [5, 10],
          },
          yAxis: {
            axisTick: {
              show: false,
            },
            axisLabel: {
              color: '#ffffff',
            },
            axisLine: {
              lineStyle: {
                color: '#ffffff',
              },
            },
          },
          legend: {
            data: ['暂无数据'],
          },
          series: [
            {
              name: '暂无数据',
              type: 'bar',
              data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
              animationDuration: 1000,
              animationEasing: 'cubicInOut',
              barWidth: '30%',
            },
          ],
        }
      } else {
        let series = []
        let legend = []
        chartData.series.forEach((item) => {
          series.push({
            name: item.name,
            type: 'bar',
            data: item.data,
            animationDuration: 1000,
            animationEasing: 'cubicInOut',
            barWidth: '40%',
            label: {
              show: true,
              position: 'outside',
            },
          })
          legend.push(item.name)
        })

        option = {
          color: ['#68cee0', '#ff6e76', '#fddd60', '#7cffb2', '#ffffff'],
          xAxis: {
            data: chartData.x,
            axisTick: {
              show: false,
            },
            axisLabel: {
              color: '#ffffff',
              interval: 0,
              rotate: 30,
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: 'rbga(0, 0, 0, 0.3)',
              },
            },
          },
          grid: {
            left: 10,
            right: 40,
            bottom: 20,
            top: 30,
            containLabel: true,
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross',
            },
            padding: [5, 10],
          },
          yAxis: {
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              color: '#ffffff',
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: 'rgba(255, 255, 255, 0.2)', // 红色网格线
                type: 'solid',
              },
            },
          },
          legend: {
            data: legend,
            textStyle: {
              color: '#ffffff',
            },
          },
          series,
        }
      }

      this.chart.setOption(option, true)
    },
    onResize() {
      if (this.chart) {
        this.chart.resize()
      }
    },
  },
}
</script>
